"""
ORM模型定义
定义数据库表结构
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, Float, Text, JSON, Boolean, TIMESTAMP, BLOB, Index
from database.connection import Base


class FaceORM(Base):
    """
    人脸信息表
    存储人脸元数据和特征向量
    """
    __tablename__ = 'faces'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID')
    face_id = Column(String(64), unique=True, nullable=False, index=True, comment='人脸唯一ID')
    person_id = Column(String(64), nullable=False, index=True, comment='人员ID')
    person_name = Column(String(100), nullable=True, comment='人员姓名')
    feature_vector = Column(BLOB, nullable=True, comment='特征向量(512维)')
    quality_score = Column(Float, nullable=True, comment='质量分数')
    face_rect = Column(JSON, nullable=True, comment='人脸框坐标')
    image_path = Column(String(500), nullable=True, comment='图片路径')
    meta_info = Column(JSON, nullable=True, comment='扩展信息')
    is_active = Column(Boolean, default=True, nullable=False, index=True, comment='是否启用')
    created_at = Column(TIMESTAMP, default=datetime.now, nullable=False, comment='创建时间')
    updated_at = Column(
        TIMESTAMP,
        default=datetime.now,
        onupdate=datetime.now,
        nullable=False,
        comment='更新时间'
    )

    # 定义复合索引
    __table_args__ = (
        Index('idx_person_active', 'person_id', 'is_active'),
        {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8mb4'}
    )

    def __repr__(self):
        return f"<FaceORM(id={self.id}, face_id={self.face_id}, person_id={self.person_id})>"
